package com.travel.server.manager.impl.command.order;

import com.travel.common.CommonConstant;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.order.QueryRefundOrderDetailRequest;
import com.travel.server.manager.dto.response.order.QueryRefundOrderDetailResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.*;
import com.travel.server.manager.impl.sal.OrderSal;
import com.travel.server.manager.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/5 18:20
 * @Desc:
 */
@Component
@Slf4j
public class QueryRefundOrderDetailCommand extends AbstractCommand<QueryRefundOrderDetailRequest, QueryRefundOrderDetailResponse> {

    @Autowired
    private RefundDal refundDal;

    @Autowired
    private OrderDal orderDal;

    @Autowired
    private SpecialDal specialDal;

    @Autowired
    private ProjectDal projectDal;

    @Autowired
    private CustomerDal customerDal;

    @Override
    protected void checkInput(QueryRefundOrderDetailRequest request) {
        if(request.getId() == null){
            throwInvalidParam();
        }
    }

    @Override
    public QueryRefundOrderDetailResponse handle(QueryRefundOrderDetailRequest request) {
        checkInput(request);

        Integer id = request.getId();
        OrderRefundInfo refundInfo = refundDal.selectById(id);

        Integer orderId = refundInfo.getOrderId();
        OrderInfo orderInfo = orderDal.selectById(orderId);

        Integer specialId = orderInfo.getSpecialId();
        ProjectSpecial special = specialDal.selectById(specialId);
        if(special == null){
            log.error("未查询到规格记录, id {}", specialId);
            throw new WorkException(WorkCode.RECORD_NOT_EXIST);
        }

        Integer projectId = special.getProjectId();
        ProjectInfo projectInfo = projectDal.selectById(projectId);
        if(projectInfo == null){
            log.error("未查询到项目记录, id {}", projectId);
            throw new WorkException(WorkCode.RECORD_NOT_EXIST);
        }

        Integer customerId = orderInfo.getCustomerId();
        CustomerInfo customerInfo = customerDal.selectById(customerId);

        return makeResponse(refundInfo, orderInfo, projectInfo, special, customerInfo);
    }

    private QueryRefundOrderDetailResponse makeResponse(OrderRefundInfo refundInfo, OrderInfo orderInfo, ProjectInfo projectInfo, ProjectSpecial special, CustomerInfo customerInfo){
        QueryRefundOrderDetailResponse response = new QueryRefundOrderDetailResponse();
        response.setApplyTime(refundInfo.getBuyerTime());
        if(refundInfo.getBusinessAmount() != null) {
            response.setBusinessAmount(CommonUtil.fenToYuan(refundInfo.getBusinessAmount()).toString());
        }
        response.setBusinessResult(refundInfo.getBusinessResult());
        response.setBusinessTime(refundInfo.getBusinessTime());
        response.setBuyer(customerInfo.getName());
        response.setBuyerHandleType(refundInfo.getBuyerHandleType());
        response.setBuyerMobile(customerInfo.getMobile());
        if(refundInfo.getBuyerRefundAmount() != null) {
            response.setBuyerRefundAmount(CommonUtil.fenToYuan(refundInfo.getBuyerRefundAmount()).toString());
        }
        response.setBuyerRefundExplains(refundInfo.getBuyerRefundExplains());
        response.setBuyerResult(refundInfo.getBuyerResult());
        response.setBuyerRefundReason(refundInfo.getRefundReason());
        response.setBuyerTime(refundInfo.getBuyerTime());
        response.setCommodityName(projectInfo.getProjectGroup() + CommonConstant.SPILT_STR + projectInfo.getName());
        if(orderInfo.getDiscountAmount() != null) {
            response.setDiscountAmount(CommonUtil.fenToYuan(orderInfo.getDiscountAmount()).toString());
        }
        response.setExpectResult(refundInfo.getExpectResult());
        if(orderInfo.getFreightAmount() != null) {
            response.setFreightAmount(CommonUtil.fenToYuan(orderInfo.getFreightAmount()).toString());
        }
        response.setLogisticsInfo(orderInfo.getLogisticsInfo());
        response.setOrderId(orderInfo.getOrderId());
        response.setOrderState(orderInfo.getOrderState());
        response.setOrderTotal(orderInfo.getOrderTotal());
        response.setProjectPicUrl(projectInfo.getProjectPicUrl());
        response.setPayTime(orderInfo.getPayTime());
        if(refundInfo.getRefundAmount() != null) {
            response.setRefundAmount(CommonUtil.fenToYuan(refundInfo.getRefundAmount()).toString());
        }
        response.setRefundSuccessTime(refundInfo.getRefundSuccessTime());
        response.setRightId(refundInfo.getRefundId());
        response.setRightReason(refundInfo.getRefundReason());
        response.setSpecialNum(special.getId() + CommonConstant.EMPTY_STR);
        Long totalAmount = OrderSal.getTotalAmount(orderInfo);
        if(totalAmount != null){
            response.setTotalAmount(CommonUtil.fenToYuan(totalAmount).toString());
        }
        if(orderInfo.getTravelStartTime() != null && orderInfo.getTravelEndTime() != null) {
            response.setTravelDate(DateUtil.dateToStr(orderInfo.getTravelStartTime(), DateUtil.YYYY_MM_DD_HH_MM_SS) + " - " + DateUtil.dateToStr(orderInfo.getTravelEndTime(), DateUtil.YYYY_MM_DD_HH_MM_SS));
        }
        return response;
    }
}
